package org.jeecg.modules.basicStationInfo.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.basicStationInfo.entity.BasicStationInfo;
import org.jeecg.modules.basicStationInfo.service.IBasicStationInfoService;
import org.jeecg.modules.system.util.RMSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 基础数据-工作站信息
 * @Author: jeecg-boot
 * @Date: 2024-09-30
 * @Version: V1.0
 */
@Api(tags = "基础数据-工作站信息")
@RestController
@RequestMapping("/basicStationInfo/basicStationInfo")
@Slf4j
public class BasicStationInfoController extends JeecgController<BasicStationInfo, IBasicStationInfoService> {
    @Autowired
    private IBasicStationInfoService basicStationInfoService;
    @Autowired
    private RMSUtil rmsUtil;

    /**
     * 分页列表查询
     *
     * @param basicStationInfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "基础数据-工作站信息-分页列表查询")
    @ApiOperation(value = "基础数据-工作站信息-分页列表查询", notes = "基础数据-工作站信息-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(BasicStationInfo basicStationInfo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<BasicStationInfo> queryWrapper = QueryGenerator.initQueryWrapper(basicStationInfo, req.getParameterMap());
        Page<BasicStationInfo> page = new Page<BasicStationInfo>(pageNo, pageSize);
        IPage<BasicStationInfo> pageList = basicStationInfoService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param basicStationInfo
     * @return
     */
    @AutoLog(value = "基础数据-工作站信息-添加")
    @ApiOperation(value = "基础数据-工作站信息-添加", notes = "基础数据-工作站信息-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody BasicStationInfo basicStationInfo) {
        this.checkColumns(basicStationInfo);
        basicStationInfoService.save(basicStationInfo);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param basicStationInfo
     * @return
     */
    @AutoLog(value = "基础数据-工作站信息-编辑")
    @ApiOperation(value = "基础数据-工作站信息-编辑", notes = "基础数据-工作站信息-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody BasicStationInfo basicStationInfo) {
        this.checkColumns(basicStationInfo);
        basicStationInfoService.updateById(basicStationInfo);
        return Result.OK("编辑成功!");
    }

    public void checkColumns(BasicStationInfo basicStation) {
        Assert.isTrue(StringUtils.isNotBlank(basicStation.getStationCode()), "请输入工作站编码");
        Assert.isTrue(StringUtils.isNotBlank(basicStation.getStationName()), "请输入工作站名称");
        Assert.isTrue(StringUtils.isNotBlank(basicStation.getStationPoint()), "请输入工作站点位编码");

        QueryWrapper<BasicStationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(null != basicStation.getId(), "id", basicStation.getId());
        queryWrapper.eq("station_code", basicStation.getStationCode());
        int repeatCount = basicStationInfoService.count(queryWrapper);
        Assert.isTrue(repeatCount == 0, "工作站编码已存在");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "基础数据-工作站信息-通过id删除")
    @ApiOperation(value = "基础数据-工作站信息-通过id删除", notes = "基础数据-工作站信息-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        basicStationInfoService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "基础数据-工作站信息-批量删除")
    @ApiOperation(value = "基础数据-工作站信息-批量删除", notes = "基础数据-工作站信息-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.basicStationInfoService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "基础数据-工作站信息-通过id查询")
    @ApiOperation(value = "基础数据-工作站信息-通过id查询", notes = "基础数据-工作站信息-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        BasicStationInfo basicStationInfo = basicStationInfoService.getById(id);
        if (basicStationInfo == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(basicStationInfo);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param basicStationInfo
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BasicStationInfo basicStationInfo) {
        return super.exportXls(request, basicStationInfo, BasicStationInfo.class, "基础数据-工作站信息");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BasicStationInfo.class);
    }

    /**
     * 工作站列表
     * @return
     */
    @AutoLog(value = "基础数据-工作站信息-列表查询")
    @ApiOperation(value = "基础数据-工作站信息-列表查询", notes = "基础数据-工作站信息-列表查询")
    @GetMapping("/getStations")
    public Result<?> getStations() {
        List<BasicStationInfo> list = basicStationInfoService.list();
        return Result.OK(list);
    }

    /**
     * 同步RMS的工作站点位信息
     */
    @AutoLog(value = "基础数据-工作站信息-工作站点位同步")
    @ApiOperation(value = "基础数据-工作站信息-工作站点位同步", notes = "基础数据-工作站信息-工作站点位同步")
    @GetMapping(value = "/synStationPoint")
    @Transactional(rollbackFor = Exception.class)
    public Result<?> synStationPoint() {
        JSONObject post = rmsUtil.cellStationPoint();// 发送请求
        JSONObject response = post.getJSONObject("response");
        JSONObject postBody = response.getJSONObject("body");
        JSONArray stations = postBody.getJSONArray("stations");
        // 封装RMS接口返回的工作站信息
        List<BasicStationInfo> basicStationInfoList = new ArrayList<>();
        for (int i = 0; i < stations.size(); i++) {
            JSONObject station = stations.getJSONObject(i);
            BasicStationInfo basicStationInfo = new BasicStationInfo();
            basicStationInfo.setStationCode(station.getString("stationId"));
            basicStationInfo.setStationPoint(station.getString("locationCellCode"));
            basicStationInfo.setStationName("默认名称");
            basicStationInfoList.add(basicStationInfo);
        }
        Assert.notEmpty(basicStationInfoList, "同步结果为0！");
        // 清空工作站点位表中数据后,新增RMS接口返回的工作站点位信息
        basicStationInfoService.remove(new QueryWrapper<>());
        boolean result = basicStationInfoService.saveBatch(basicStationInfoList);
        Assert.isTrue(result, "同步数据库失败！");

        return Result.OK("同步成功,工作站名称需根据实际业务需求进行调整", basicStationInfoList);
    }

}
